package com.wnhz.work.jy.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.ProductAdapter;
import com.wnhz.work.jy.fragment.good.DeriveFragment;
import com.wnhz.work.jy.fragment.good.ForeignFragment;
import com.wnhz.work.jy.fragment.good.PandaFragment;
import com.wnhz.work.jy.fragment.good.SouvenirFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * ------产品中心
 */
public class ProductActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TabLayout tabLayout;
    private ViewPager vp_product;
    private FragmentPagerAdapter mAdapter;

    private List<Fragment> list_fragment; //定义要装fragment的列表
    private List<String> list_title; //tab名称列表

//    private RecommendFragment fragment1; //推荐
    private PandaFragment fragment2; //熊猫
    private SouvenirFragment fragment3; //纪念
    private DeriveFragment fragment4; //衍生品
    private ForeignFragment fragment5; //外国币

    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);

        initView();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("产品中心");

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        vp_product = (ViewPager) findViewById(R.id.vp_product);

        findViewById(R.id.btn_main_left).setOnClickListener(this);
       /* findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        findViewById(R.id.view_main_right_image).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_right).setOnClickListener(this);*/

        //初始化各fragment
//        fragment1 = new RecommendFragment();
        fragment2 = new PandaFragment();
        fragment3 = new SouvenirFragment();
        fragment4 = new DeriveFragment();
        fragment5 = new ForeignFragment();

        //将fragment装进列表中
        list_fragment = new ArrayList<>();
//        list_fragment.add(fragment1);
        list_fragment.add(fragment2);
        list_fragment.add(fragment3);
        list_fragment.add(fragment4);
        list_fragment.add(fragment5);

        //将名称加载tab名字列表
        list_title = new ArrayList<>();
//        list_title.add("推荐");
        list_title.add("熊猫币");
        list_title.add("纪念币");
        list_title.add("衍生品");
        list_title.add("外国币");

        //设置TabLayout的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        //为TabLayout添加tab名称
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(2)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(3)));
//        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(4)));

        mAdapter = new ProductAdapter(ProductActivity.this.getSupportFragmentManager(), list_fragment, list_title);

        //viewpager加载adapter
        vp_product.setAdapter(mAdapter);

        //TabLayout加载viewpager
        tabLayout.setupWithViewPager(vp_product);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

//            case R.id.btn_main_right:
//                Toast.makeText(ProductActivity.this, "搜索", Toast.LENGTH_SHORT).show();
//                break;

            default:
                break;
        }
    }

}
